import util from '../../utils/util'
import {
    GetMenuList,
    MenuArticle,

    GetArticle,
    GetArticleList,
    GetColumnContent,
    GetListMlink
} from '../../config/api'
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        pid: '',
        aprams: '',
        isTab: true,
        isflag: 0,

        id: '', // 单页文章id
        is_flag: '',
        type: '',
        active: 0, //当前激活选中项，默认为0
        columns: [], //栏目数组
        richText: '', //富文本数据内容
        imgarr: [],
        page: 1,
        limit: 15,
        lists: [], // 列表数组
        isEmpty: false,
        isNoColumn: false,
        show: '', // 是否是村情简介 默认1
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let that = this;
        console.log('参数', options)
        console.log('Object.keys(options):', Object.keys(options)[1]);
        console.log('Object.values(options))', Object.values(options)[1]);
        this.setData({
            pid: options.pid,
            aprams: Object.keys(options)[1] + "=" + Object.values(options)[1]
        })
        console.log('this.data.aprams:', this.data.aprams);

        if (options.active >= 0) {
            this.setData({
                active: options.active
            })
        }
        if (options.show) {
            this.setData({
                show: options.show
            })
        }
        console.log('show', options.show)
        this.getTabList()
    },

    getTabList() {
        let that = this;
        util.apiRequest(GetMenuList, {
            type: "mini",
            pid: that.data.pid,
            vollege_id: app.globalData.vollege_id
        }, null).then(r => {
            console.log("tab栏目数据：", r);
            if (r.data.data.length > 0) {
                that.checkType(r.data.data[that.data.active])
                that.setData({
                    isTab: true,
                    columns: r.data.data,
                    aprams: r.data.data[that.data.active].param
                })
            } else {
                that.getParams();
                that.setData({
                    isTab: false
                })
            }
            that.getArticleList();
        });
    },

    getParams() {
        let that = this;
        util.apiRequest("/api/MenuArticle/getMenuDetail", {
            id: that.data.pid,
        }, null).then(r => {
            console.log("参数r:", r);
            that.setData({
                aprams: r.data.data.param
            })
        });
    },

    getArticleList() {
        let that = this;
        let fileds = that.data.aprams.split('=');
        let params = {
            category: fileds[1],
            vollege_id: app.globalData.vollege_id
        }
        util.apiRequest("/api/MenuArticle/getArticleList?", params, null).then(r => {
            console.log("详情列表：", r);
            let state = r.data.data.length;
            switch (state) {
                case 0:
                    that.setData({
                        isflag: 2
                    })
                    break;
                case 1:
                    let newContent = r.data.data[0].content.replace(/<img/g, '<img class="richImg"');
                    let imgarr = that.richImg(newContent)
                    console.log('imgarr:', imgarr)

                    that.setData({
                        isflag: 1,
                        richText: newContent,
                        imgarr
                    })
                    break;
                default:
                    that.setData({
                        isflag: 0,
                        lists: r.data.data
                    })
            }
        });
    },

    // 富文本图片放大
    richImg(txt) {//txt 指的是接口返回的富文本
        // 主要代码
        let imgarr = [];
        let regex = new RegExp(/<img.*?(?:>|\/>)/gi); // 匹配所有图片
        let srcReg = /src=[\'\"]?([^\'\"]*)[\'\"]?/i; // 匹配src图片
        let arrsImg = txt.match(regex);
        for (let i = 0; i < arrsImg.length; i++) {
            let srcs = arrsImg[i].match(srcReg);
            imgarr.push(srcs[1])
        }
        return imgarr  //把富文本里的图片返回
    },

    catchImage(e) {
        console.log(e);
        wx.previewImage({
            current: this.data.imgarr[0], // 当前显示图片的http链接
            urls: this.data.imgarr // 需要预览的图片http链接列表
        })
    },

    /**
     * 改变切换选项卡默认激活状态
     */
    SwitchStatus: function (e) {
        // 传递的参数
        let index = e.currentTarget.dataset['index'];
        let item = e.currentTarget.dataset['item'];
        console.log('切换点击item：', item)
        this.setData({
            active: index
        })
        this.checkType(item)
    },

    checkType(item) {
        console.log("点击tab:", item)
        let flag = item.content_type;
        if (flag == "url") {
            // 跳转链接传参
            wx.redirectTo({
                url: item.url + "?" + item.param + '&id=' + item.id + '&pid=' + item.pid
            })
        } else {
            this.setData({
                aprams: item.param
            })
            this.getArticleList()
        }
    },

    /**
     * 跳转到详情页面
     */
    ToMessageDetail: function (e) {
        let item = e.currentTarget.dataset['item'];
        console.log("点击事件传递的参数：", item.id);
        wx.navigateTo({
            url: '/pages/newsDetail/newsDetail?id=' + item.id
        });
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        var that = this;
        var page = this.data.page;
        page++;
        that.setData({
            page: page
        })

        // this.getArticleList()
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})